import {
	deviceIsBuy,
	deviceTypeIcon,
	groupExpTime,
	doorlock
} from '@/utils/index'
import {
	appletDeviceList,
	appletDeviceList2,
	AsMsgQ
} from '@/API/api.js'
import logins from '@/utils/loginApi.js'
const {handleLogin,httpReturn} = logins();
export default {
	data() {
		return {
			buttonMessage: {
				fail: false, //true表示请求成功
				message: {},
				channelMessage: {}, //多通道信息
			},
			classifyId: [], //商品标识
			appDevices: [], //app返回的设备信息
			modalName: [],
			supportDeviceObj: {
				supportDevice: [], //支持的设备
				TabCur: -1,
				scrollLeft: 0,
			},
			isDoorbells:null
		}
	},
	watch: {
		appDevices: {
			handler(newVal) {
				if (newVal.length) {
					this.supportDeviceObj.supportDevice = newVal.filter(t => {
						return t.supportDevice
					})
					if (this.supportDeviceObj.supportDevice.length) {
						let item = this.supportDeviceObj.supportDevice[0];
						this.handleSelectDevice(item, 0)
					}
				}
			},
			deep: true
		}
	},
	onLoad(e) {
		handleLogin((res) => {
			this.classifyId = e.classifyId;
			this.defaultData();
			this.getAppDeviceList()
		})
		// this.classifyId = e.classifyId;
		// this.defaultData();
		// this.getAppDeviceList()
	},
	onShow() {

	},
	methods: {
		//  初始化数据
		defaultData() {
			this.buttonMessage = {
				fail: false,
				message: {},
				channelMessage: {}
			}
			this.modalName = ''
		},
		// 获取设备数据
		getAppDeviceList() {
			let header = {
				Authorization: uni.getStorageSync('accessToken'),
				appKey: getApp().globalData.appConfig.xm_appKey
			}
			appletDeviceList2(header,{limit:999,page:1}).then(res => {
				let data = res.data.data || []
				let devList = [];
				if (data && data.length) {
					data.forEach(z => {
						devList.push({
							devId: z.deviceNo,
							devName: z.deviceName,
							devType: z.pid,
							devState: 0,
							u:z.devUserName,
							p:z.devPassWord,
							adminToken:z.adminToken,
							userGroupId:z.userGroupId,
							roomId:z.roomId,
							devIcon:z.devicePic,
							pid:z.pid
							
						})
					})
				}
				this.deviceMessage(devList, this.classifyId)
			});
		},
		// 设备信息
		deviceMessage(devList, classifyId, callback) {
			this.appDevices = devList.map((t, i) => {
				t.icon = deviceTypeIcon(t.devType);
				t.supportDevice = false;
				let deviceId = t.devId;
				let data = {
					"body": {
						"caps": [classifyId],
						"sn": t.devId
					},
					"method": "reqCaps"
				}
				AsMsgQ(data).then(res => {
					const {
						ret,
						caps
					} = res;
					if (ret === 200) {
						// 判断是否支持此classifyId
						let keyName = classifyId === 'xmc.css' ? `${classifyId}.vid.support` :
							`${classifyId}.support`;
						let arr = Object.keys(caps); //key值
						arr.forEach(arr_key => {
							// 判断是否购买以及购买是否到期
							if (arr_key === `${classifyId}.expirationtime` || arr_key ===
								`${classifyId}.vid.expirationtime`) {
								if (caps[arr_key]) {
									let isExpireMessage = deviceIsBuy(caps[arr_key] * 1000);
									t.expireTime = isExpireMessage.time;
									t.expirationtimeColor = isExpireMessage.color;
								}
							}
							// 判断设备是不是多通道，以及多通道处理
							if (arr_key === `${classifyId}.vid.expirationtime.channels`) {
								let channelsArr = caps[arr_key] ? caps[arr_key].split('_') :
							[]; //获取各个通道的时间
								t['maxchannel'] = caps['xmc.css.maxchannel']; //最大通道数
								t.channelsArr = []; //初始化重组数据
								t.normalUseChannel = 0; //初始化通道正常使用的数量
								t.expireUseChannel = 0; //初始化通道到期使用的数量
								// 云存储中多通道图标且设备类型为0改为NVR
								if (classifyId === 'xmc.css') {
									if (channelsArr[0] && t.devType == 0) {
										t.icon = '0x3';
									}
								}
								channelsArr.forEach((CHANNEL_TIME, CHANNEL_i) => {
									if (CHANNEL_TIME) {
										if (CHANNEL_TIME && CHANNEL_TIME != '0' &&
											CHANNEL_TIME != '-1') {
											let ExpTime = groupExpTime(CHANNEL_TIME *
												1000);
											t.channelsArr.push({
												chnId: Number(CHANNEL_i),
												expireTime: ExpTime
											});
											// 获取正常使用的通道数
											if (ExpTime.color != 'reds') {
												t.normalUseChannel++ //正常使用的数量
											} else {
												t.expireUseChannel++ //到期使用的数量
											}
										} else {
											t.channelsArr.push({
												chnId: Number(CHANNEL_i),
												expireTime: 0
											})
										}
									}
								})
							}
							// 判断支持此能力集的设备
							if (arr_key === keyName) {
								if (caps[arr_key]) {
									t.supportDevice = true;
								}
							}
						})

					}

				})
				return t
			})
		},
		// 设备的宽度
		selectDevice(item, index) {
			let width = (uni.getSystemInfoSync().screenWidth - 60) / 3;
			this.supportDeviceObj.TabCur = index;
			this.supportDeviceObj.scrollLeft = (index - 1) * width; //向左滚动距离
		},
		// 设备选择
		handleSelectDevice(item, index) {
			this.modalName = '';
			this.selectDevice(item, index);
			const {
				channelsArr,
				devId,
				normalUseChannel
			} = item;
			if (item['maxchannel'] && item['maxchannel'] > 0) {
				// 多通道
				this.buttonMessage = {
					fail: true,
					message: {},
					channelMessage: {
						classifyId: this.classifyId,
						sn: devId,
						channelsArr,
						'xmc.css.maxchannel': item['maxchannel'],
						normalUseChannel,
						pid:item.pid ? item.pid : ''
					}
				}
			} else {
				// 单通道
				this.buttonMessage = {
					fail: true,
					message: item,
					channelMessage: {}
				}
			}
			let pid = item.pid ? item.pid : '';
			this.isDoorbells = doorlock().some(item=>item===pid)

		}
	}
}
